package com.zhida.smartcontrol.pagehome.hdimmerdialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.SeekBar;

import com.zhida.smartcontrol.R;
import com.zhida.smartcontrol.common.Const;
import com.zhida.smartcontrol.common.bean.LampBean;
import com.zhida.smartcontrol.mvp.MVPBaseDialogFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import freemarker.ext.beans.BeanModel;

/**
 * author: tanjianpei
 * date: 2019/12/13 013 10:57
 * description: 调光
 */
public class HDimmerDialogFragment extends MVPBaseDialogFragment<HDimmerDialogContract.View, HDimmerDialogPresenter> implements HDimmerDialogContract.View {
    @BindView(R.id.seek_bar)
    SeekBar mSeekBar;

    private String mType;
    private String mSwitchType;
    private LampBean mLampBean;

    public static HDimmerDialogFragment newInstance(String switchType, LampBean data) {
        Bundle args = new Bundle();
        args.putString("type", switchType);
        args.putParcelable("bean", data);
        HDimmerDialogFragment fragment = new HDimmerDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected Object getLayout() {
        return R.layout.dialog_dimmer;
    }

    @Override
    protected void initDatas(Bundle savedInstanceState) {
        Bundle bundle = getArguments();
        if (null == bundle) {
            return;
        }
        mType = "3";
        mSwitchType = bundle.getString("type");
        mLampBean = bundle.getParcelable("bean");
    }

    @OnClick({R.id.btn_cancel, R.id.btn_confirm})
    public void click(View v) {
        switch (v.getId()) {
            case R.id.btn_cancel:
                dismiss();
                break;
            case R.id.btn_confirm:
                int type = Integer.parseInt(mSwitchType);
                if (type == Const.PLC_SINGLE || type == Const.ZIG_BEE_SINGLE) {
                    mPresenter.plcBrightness(mLampBean, mType);
                } else {
                    List<String> code=new ArrayList<>();
                    code.add(mLampBean.getCode());
                    if (type==Const.NB_SINGLE){
                        mPresenter.nbBrightness(code, "", mType);
                    }else {
                        singleDialog(code);
                    }
                }
                break;
        }
    }
    public void singleDialog(List<String> code) {
        final String data[] = {"主灯", "辅灯", "主灯辅灯"};
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext())

                .setTitle("请选择你想操作的灯")

                .setSingleChoiceItems(data, -1, (dialogInterface, i) -> {
                    switch (i) {
                        case 0:
                            mPresenter.nbBrightness(code, "1", mType);
                            break;
                        case 1:
                            mPresenter.nbBrightness(code, "2", mType);
                            break;
                        case 2:
                            mPresenter.nbBrightness(code, "3", mType);
                            break;
                        default:
                            showMsg("请选择你想操作的灯");
                            break;
                    }

                    dialogInterface.dismiss();
                });
        builder.show();
    }
    @Override
    protected void initEvents(Dialog dialog) {
        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                //1：100%         2：75%       3：50%       4：25%       0：0%
                if (progress < 25) {
                    progress = 0;
                    mSeekBar.setProgress(progress);
                    mType = "0";
                } else if (progress < 50) {
                    progress = 25;
                    mType = "4";
                    mSeekBar.setProgress(progress);
                } else if (progress < 75) {
                    progress = 50;
                    mType = "3";
                    mSeekBar.setProgress(progress);
                } else if (progress < 100) {
                    progress = 75;
                    mType = "2";
                    mSeekBar.setProgress(progress);
                } else {
                    progress = 100;
                    mType = "1";
                    mSeekBar.setProgress(progress);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    @Override
    public void onSuccess() {
        dismiss();
    }

}
